import os, time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import urllib.request

#远程调试
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")

driver = webdriver.Chrome(options=chrome_options)

driver.get('https://www.baidu.com')

driver.find_element_by_xpath('//*[@id="kw"]').send_keys('深圳')
driver.find_element_by_xpath('//*[@id="su"]').click()
time.sleep(2)
driver.find_element_by_link_text('图片').click()

time.sleep(2)
#定位图片元素
images = lambda x:x.find_elements_by_css_selector('div.imgbox')

#当页面获取到的图片数少于50，则滚动鼠标中键
while len(images(driver)) < 50:
    #通过调用js来滚动鼠标中键
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
    time.sleep(1)

#点击第49张图片
images(driver)[49].click()








